<?php
namespace App\Service\Admin;

use SebastianBergmann\CodeCoverage\Report\Xml\Totals;

class TongjiService1114 extends BaseService
{
    //统计

    //个人
    public static function person($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
        if (in_array($pas['urole_id'], [3,4])) {
            if ($pas['is_leader']) { //部门负责人
                $db = $db->where('depart_id', $pas['udepart_id']);
            } else { //部门员工
                $db = $db->where('staff_id', $pas['ustaff_id']);
            }
        } else if (in_array($pas['urole_id'], [1,2,5,6])) { //管理员|总经理|审核员|统计员
            if ($pas['depart_id']) { //下拉选择
                $db = $db->where('depart_id', $pas['depart_id']);
            }
        }
        if ($pas['staff_id']) {
            $db = $db->where('staff_id', $pas['staff_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01';
            $endtime = $pas['year'].'-'.$month.'-31';
            $db = $db->where('in_day', '>=', $starttime);
            $db = $db->where('in_day', '<=', $endtime);
        }
        $db = $db->where('status', '>', 4);
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('in_day', 'desc');
        } else {
            $db = $db->orderBy('in_day', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '' : $staffOne['title'];
            $delay = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->sum('delay');
            // $delay = $v->delay;
            $shi = floor($delay / 60);
            $fen = $delay - $shi * 60;
            $delay_str = $shi . '小时' . $fen . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'uname' => $staff_name, 'item_name' => $item_name,
                'delay' => $delay_str, 'in_day' => $v->in_day,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    // 获取要导出的数据
    public static function getOutData($pas)
    {
        $year = $pas['year'];
        $month = $pas['month'];
        // 标题
        $headers = ['月份：'.$month.'月','',''];
        $titles = ['部门', '员工', '项目', '工时分配'];
        $days = [];
        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
        for ($i = 1; $i <= $day_num; $i ++) {
            $titles[] = $i;
            $days[] = $i;
        }
        $titles[] = '合计';
        $arys[] = $headers;
        $arys[] = $titles;
        $fname = $month.'月工时汇总';
        // 工时
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
        if (in_array($pas['urole_id'], [3,4])) {
            if ($pas['is_leader']) { //部门负责人
                $db = $db->where('depart_id', $pas['udepart_id']);
            } else { //部门员工
                $db = $db->where('staff_id', $pas['ustaff_id']);
            }
        } else if (in_array($pas['urole_id'], [1,2,5,6])) { //管理员|总经理|审核员|统计员
            if ($pas['depart_id']) { //下拉选择
                $db = $db->where('depart_id', $pas['depart_id']);
            }
        }
        if ($pas['staff_id']) {
            $db = $db->where('staff_id', $pas['staff_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01';
            $endtime = $pas['year'].'-'.$month.'-31';
            $db = $db->where('in_day', '>=', $starttime);
            $db = $db->where('in_day', '<=', $endtime);
        }
        $db = $db->where('status', '>', 4);
        $db = $db->groupBy('staff_id');
        $res = $db->orderBy('depart_id', 'asc')
            ->orderBy('staff_id', 'asc')
            ->orderBy('item_id', 'asc')
            ->get();
        if (!count($res)) {
            $rst = [
                'data' => $arys,
                'fname' => $fname,
            ];
            return self::toback(0, '', $rst);
        }
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '' : $staffOne['title'];
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $whOnes = self::dbConn(self::$dbYwWh)
                ->where('del', 0)
                ->where('staff_id', $v->staff_id)
                ->where('status', '>', 4)
                ->get();
            $wh_ids = [];
            foreach ($whOnes as $vv) {
                if (!in_array($vv->id, $wh_ids)) {
                    $wh_ids[] = $vv->id;
                }
            }
            $wiOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                // ->where('wh_id', $v->id)
                ->whereIn('wh_id', $wh_ids)
                ->groupBy('config_whid')
                ->orderBy('config_whid', 'asc')
                ->get();
            $heji = []; //员工合计
            if (count($wiOnes)) {
                foreach ($wiOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        // ->where('del', 0)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $wh_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $row = [
                        $depart_name, $staff_name, $item_name, $wh_name,
                    ];
                    // ddd($row);
                    // 员工日工时
                    $month_total = 0;
                    foreach ($days as $day) {
                        $heji[$day] = isset($heji[$day]) ? $heji[$day] : 0; //工时合计
                        $day_str = strlen($day)==1 ? '0'.$day : $day;
                        $daystr = $year.'-'.$month.'-'.$day_str;
                        $delay = self::dbConn(self::$dbYwWhItem.' as a')
                            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                            ->where('a.del', 0)
                            ->where('a.uid', $v->staff_id)
                            ->where('a.config_whid', $vv->config_whid)
                            ->where('b.item_id', $v->item_id)
                            ->where('b.in_day', $daystr)
                            ->where('b.status', '>', 4)
                            ->sum('a.delay');
                        $month_total += $delay;
                        $row[] = $delay;
                        $heji[$day] += $delay;
                    }
                    $row[] = $month_total;
                    $arys[] = $row;
                }
            }
            // 员工合计
            $u_row = [
                $depart_name, $staff_name, '项目合计', '工时合计',
            ];
            $u_delay = 0;
            foreach ($days as $day) {
                $day_heji = $heji[$day];
                $u_row[] = $day_heji;
                $u_delay += $day_heji;
            }
            $u_row[] = $u_delay;
            $arys[] = $u_row;
        }
        // ddd($arys);
        $rst = [
            'data' => $arys,
            'fname' => $fname,
        ];
        return self::toback(0, '', $rst);
    }

    /** 
     * 项目组
     * 部门经理看本部门的
     * 经理、管理员、审核员看所有的
     * 每个部门对应的各个项目的工时分配
     * */
    public static function item($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
        $db = $db->whereIn('status', [5,6,7]);
        if (in_array($pas['role_id'], [1,2,5,6])) { //管理员|总经理|审核员|统计员
            if ($pas['depart_id']) {
                $db = $db->where('depart_id', $pas['depart_id']);
            }
            if ($pas['staff_id']) {
                $db = $db->where('staff_id', $pas['staff_id']);
            }
        } else if (in_array($pas['role_id'], [3,4])) {
            if ($pas['is_leader']) { //部门经理
                $db = $db->where('depart_id', $pas['udepart_id']);
            } else {
                $db = $db->where('staff_id', $pas['uid']);
            }
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01';
            $endtime = $pas['year'].'-'.$month.'-31';
            $db = $db->where('in_day', '>=', $starttime);
            $db = $db->where('in_day', '<=', $endtime);
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('in_day', 'desc');
        } else {
            $db = $db->orderBy('in_day', 'asc');
        }
        $db = $db->orderBy('depart_id', 'asc');
        $db = $db->groupBy('item_id');
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $delay_total = 0;
            $wh_ids = [];
            if ($pas['month']) {
                $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
                $starttime = $pas['year'].'-'.$month.'-01';
                $endtime = $pas['year'].'-'.$month.'-31';
                $whOnes = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    // ->where('staff_id', $v->staff_id)
                    ->where('item_id', $v->item_id)
                    ->where('status', '>', 4)
                    ->where('in_day', '>=', $starttime)
                    ->where('in_day', '<=', $endtime)
                    ->get();
                foreach ($whOnes as $vv) {
                    $wh_ids[] = $vv->id;
                } 
            } else {
                $wh_ids[] = $v->id;
            }
            $whList = [];
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->whereIn('wh_id', $wh_ids)
                ->orderBy('id', 'desc')
                ->get();
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $uOne = (array) self::dbConn(self::$dbStaff)
                        ->where('id', $vv->uid)
                        ->first();
                    $uname = empty($uOne) ? '' : $uOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟' . implode(',',$wh_ids);
                    $delay_total += $vv->delay;
                    $whList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                        'uname' => $uname,
                        'start' => $vv->starthour,
                        'end' => $vv->endhour,
                    ];
                }
            }
            $shi_pid = floor($delay_total / 60);
            $fen_pid = $delay_total - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'item_name' => $item_name, 'in_day' => $v->in_day,
                'whList' => $whList, 'delay_str' => $delay_pid_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //所有项目
    public static function all($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
            // ->where('delay', '>', 0);
        $db = $db->where('status', '>', 4);
        if ($pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01';
            $endtime = $pas['year'].'-'.$month.'-31';
            $db = $db->where('in_day', '>=', $starttime);
            $db = $db->where('in_day', '<=', $endtime);
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('in_day', 'desc');
        } else {
            $db = $db->orderBy('in_day', 'asc');
        }
        $db = $db->groupBy('item_id');
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $wh_ids = [];
            if ($pas['month']) {
                $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
                $starttime = $pas['year'].'-'.$month.'-01';
                $endtime = $pas['year'].'-'.$month.'-31';
                $whOnes = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('staff_id', $v->staff_id)
                    ->where('item_id', $v->item_id)
                    ->where('status', '>', 4)
                    ->where('in_day', '>=', $starttime)
                    ->where('in_day', '<=', $endtime)
                    ->get();
                foreach ($whOnes as $vv) {
                    $wh_ids[] = $vv->id;
                } 
            } else {
                $wh_ids[] = $v->id;
            }
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->whereIn('wh_id', $wh_ids)
                ->where('delay', '>', 0)
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $uOne = (array) self::dbConn(self::$dbStaff)
                        ->where('id', $vv->uid)
                        ->first();
                    $uname = empty($uOne) ? '' : $uOne['title'];
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                        'uname' => $uname,
                        'in_day' => $v->in_day,
                        'start' => $vv->starthour,
                        'end' => $vv->endhour,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'item_name' => $item_name,
                'delay_str' => $delay_pid_str, 'whList' => $itemList,
                'in_day' => $v->in_day,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //环比
    public static function huan($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
        $db = $db->whereIn('status', [5,6,7]);
        if ($pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01';
            $endtime = $pas['year'].'-'.$month.'-31';
            $db = $db->where('in_day', '>=', $starttime);
            $db = $db->where('in_day', '<=', $endtime);
        }
        $db = $db2 = $db->groupBy('item_id');
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('in_day', 'desc');
        } else {
            $db = $db->orderBy('in_day', 'asc');
        }
        $db = $db->orderBy('depart_id', 'asc');
        // $total = $db->count();
        $total = count($db->get());
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
            $wh_ids = [];
            if ($pas['month']) {
                $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
                $starttime = $pas['year'].'-'.$month.'-01';
                $endtime = $pas['year'].'-'.$month.'-31';
                $whOnes = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('staff_id', $v->staff_id)
                    ->where('item_id', $v->item_id)
                    ->where('status', '>', 4)
                    ->where('in_day', '>=', $starttime)
                    ->where('in_day', '<=', $endtime)
                    ->get();
                foreach ($whOnes as $vv) {
                    $wh_ids[] = $vv->id;
                } 
            } else {
                $wh_ids[] = $v->id;
            }
            $delay_total = 0;
            $whList = [];
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->whereIn('wh_id', $wh_ids)
                ->orderBy('id', 'desc')
                ->get();
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $uOne = (array) self::dbConn(self::$dbStaff)
                        ->where('id', $vv->uid)
                        ->first();
                    $uname = empty($uOne) ? '' : $uOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_total += $vv->delay;
                    $whList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                        'uname' => $uname,
                        'start' => $vv->starthour,
                        'end' => $vv->endhour,
                    ];
                }
            }
            $shi_pid = floor($delay_total / 60);
            $fen_pid = $delay_total - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            // 同比，上一年
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 365;
            $end = strtotime($end) - 3600 * 24 * 365;
            $delay_year = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            // if ($delay_year) {
            //     $tongbi = number_format($delay_total / $delay_year, 2);
            // } else {
            //     $tongbi = $delay_total * 100;
            // }
            // 同比：(本期-去年同期)/去年同期*100
            if ($delay_year) {
                $tongbi = ($delay_total - $delay_year) / $delay_year * 100;
                $tongbi = number_format($tongbi, 2);
            } else {
                $tongbi = $delay_total * 100;
            }
            $tongbi = $tongbi>0 ? '+' . $tongbi : '-' . $tongbi;
            // 环比，上一个月
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 30;
            $end = strtotime($end) - 3600 * 24 * 30;
            $start = date('Y-m-d', $start) . ' 00:00:01';
            $end = date('Y-m-d', $end) . ' 23:59:59';
            $delay_month = self::dbConn(self::$dbYwWhItem.' as a')
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            // if ($delay_month) {
            //     $huanbi = number_format($delay_total / $delay_month, 2);
            // } else {
            //     $huanbi = $delay_total * 100;
            // }
            // 环比：(本期-上月)/上期*100%
            if ($delay_month) {
                $huanbi = ($delay_total - $delay_month) / $delay_month * 100;
                $huanbi = number_format($huanbi, 2);
            } else {
                $huanbi = $delay_total * 100;
            }
            $huanbi = $huanbi>0 ? '+' . $huanbi : '-' . $huanbi;
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'item_name' => $item_name,
                'whList' => $whList, 'delay_str' => $delay_pid_str,
                // 同比上一年，环比上一月
                // 'delay_pid' => $delay_total, 'delay_year' => $delay_year,
                'delay_total' => $delay_total, 
                'delay_year' => $delay_year,
                'delay_month' => $delay_month,
                'tongbi' => $tongbi.'%', 'huanbi' => $huanbi.'%',
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //图
    public static function tu($pas)
    {
        $year = $pas['year'];
        $month = $pas['month'];
        $month = strlen($month)==1 ? '0'.$month : $month;
        // $daystart = $year.'-'.$month.'-01 00:00:01';
        // $dayend = $year.'-'.$month.'-31 23:59:59';
        $daystart = $year.'-'.$month.'-01';
        $dayend = $year.'-'.$month.'-31';
        // $arys = [
        //     'bing' => [['xxx', 10], ['zzz', 20]], //项目占比
        //     'zhu' => [ //工时占比
        //         'title' => ['张三', '李四'],
        //         'val' => [[10], [50]],
        //     ],
        // ];
        // return self::toback(0, '', $arys);
        // ddd($daystart, $dayend);
        $arys = ['bing' => [], 'zhu' => []];
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        $delay_item = self::dbConn(self::$dbYwWh.' as a')
            ->leftJoin(self::$dbYwWhItem.' as b', 'a.id', '=', 'b.wh_id')
            ->where('a.del', 0)
            ->where('b.del', 0)
            ->where('a.in_day', '>=', $daystart)
            ->where('a.in_day', '<=', $dayend)
            ->sum('b.delay');
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $delay = self::dbConn(self::$dbYwWh.' as a')
                    ->leftJoin(self::$dbYwWhItem.' as b', 'a.id', '=', 'b.wh_id')
                    ->where('a.del', 0)
                    ->where('b.del', 0)
                    ->where('a.item_id', $v->id)
                    ->where('a.status', '>', 4)
                    ->where('a.in_day', '<>', '')
                    ->where('a.in_day', '>=', $daystart)
                    ->where('a.in_day', '<=', $dayend)
                    ->sum('b.delay');
                $rate = floor($delay / $delay_item * 100);
                $arys['bing'][] = [
                    $v->title, $rate,
                ];

            }
        }
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->get();
        $delay_user = self::dbConn(self::$dbYwWhItem.' as a')
            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
            ->where('a.del', 0)
            ->where('b.in_day', '<>', '')
            ->where('b.in_day', '>=', $daystart)
            ->where('b.in_day', '<=', $dayend)
            ->sum('a.delay');
        if (count($staffOnes)) {
            foreach ($staffOnes as $v) {
                $delay = self::dbConn(self::$dbYwWhItem.' as a')
                    ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                    ->where('a.del', 0)
                    ->where('a.uid', $v->id)
                    ->where('b.status', '>', 4)
                    ->where('b.in_day', '<>', '')
                    ->where('b.in_day', '>=', $daystart)
                    ->where('b.in_day', '<=', $dayend)
                    ->sum('a.delay');
                $rate = floor($delay / $delay_user * 100);
                $arys['zhu']['title'][] = $v->title;
                $arys['zhu']['val'][] = [$rate];
            }
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption($pas)
    {
        $arys = ['staff' => [], 'depart' => []];
        if ($pas['role_id'] == 4) {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->where('id', $pas['depart_id'])
                ->get();
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_id', $pas['depart_id'])
                ->get();
        } else {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->get();
            if ($pas['depart_id']) {
                $staffList = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->where('depart_id', $pas['depart_id'])
                    ->get();
            } else {
                $staffList = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->get();
            }
        }
        if (count($staffList)) {
            foreach ($staffList as $k => $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'].'-';
                $staffList[$k]->title = $depart_name.$v->title;
            }
        }
        $arys['depart'] = $departList;
        $arys['staff'] = $staffList;
        return self::toback(0, '', $arys);
    }

    //项目选项
    public static function itemOption($pas)
    {
        $arys = ['staff' => [], 'depart' => []];
        if ($pas['role_id'] == 3) {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->where('id', $pas['depart_id'])
                ->get();
        } else {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->get();
        }
        if ($pas['depart_id']) {
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_id', $pas['depart_id'])
                ->get();
        } else {
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->get();
        }
        if (count($staffList)) {
            foreach ($staffList as $k => $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'].'-';
                $staffList[$k]->title = $depart_name.$v->title;
            }
        }
        $arys['depart'] = $departList;
        $arys['staff'] = $staffList;
        return self::toback(0, '', $arys);
    }
}